﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using CompanyHub.Models;
using Windows.ApplicationModel;
using Windows.Phone.Management.Deployment;

namespace CompanyHub
{
    public partial class PackagePage : PhoneApplicationPage
    {
        public PackagePage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string id = "";

            if (NavigationContext.QueryString.TryGetValue("id", out id))
            {
                DataContext = App.ViewModel.FindPackage(id);
                UpdateUI();
            }

        }
        private void UpdateUI()
        {
            var pkg = DataContext as CompanyPackage;

            switch (pkg.Status)
            {
                case CompanyPackageStatus.Installed:
                    btnInstall.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case CompanyPackageStatus.New:
                    btnLaunch.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case CompanyPackageStatus.Update:
                    btnInstall.Content = "Update";
                    break;
            }
        }
        private async void btnInstall_Click(object sender, RoutedEventArgs e)
        {
            var pkg = DataContext as CompanyPackage;
           // if (MessageBox.Show("Install " + pkg.Title + "?", "Install app", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
           // {
                try
                {
                    var result = await InstallationManager.AddPackageAsync(pkg.Title, new Uri(pkg.SourceUri));
                    if (result.InstallState == Windows.Management.Deployment.PackageInstallState.Installed)
                    {
                        MessageBox.Show(pkg.Title + " was installed.");
                        App.ViewModel.UpdatePackageStatus();
                        UpdateUI();
                    }
                    else
                    {
                        MessageBox.Show("An error occurred during installation.");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occurred during installation.");
                }
           // }
        }

        private void btnLaunch_Click(object sender, RoutedEventArgs e)
        {
            var pkg = DataContext as CompanyPackage;

            var devicePkgs = InstallationManager.FindPackagesForCurrentPublisher();

            var devicePkg = devicePkgs.Where(p => p.Id.ProductId.Contains(pkg.PackageId.ToString().ToUpper())).FirstOrDefault();

            if (devicePkg != null)
            {
                devicePkg.Launch("");
            }


        }
    }
}